
import xmlwriter
from datetime import datetime

ATOM_NS = "http://www.w3.org/2005/Atom"
THR_NS = "http://purl.org/syndication/thread/1.0"

class Feed:
    def __init__(self, id, title, updated, lang='en'):
        updated = updated.replace(microsecond=0)
        self.w = xmlwriter.XmlWriter()
        self.w.startElement('feed', ATOM_NS)
        self.w.attribute('lang', lang, 'xml')
        self.w.contentElement('id', id, ATOM_NS)
        self.w.contentElement('title', title, ATOM_NS)
        self.w.contentElement('updated', updated.isoformat(), ATOM_NS)
        
    def add_author(self, name, uri=None, email=None):
        self.w.startElement('author', ATOM_NS)
        self.w.contentElement('name', name, ATOM_NS)
        if uri:
            self.w.contentElement('uri', uri, ATOM_NS)
        if email:
            self.w.contentElement('email', email, ATOM_NS)
        self.w.endElement()

    def close(self):
        self.w.close()

    def get(self):
        return self.w.buf.getvalue()

if __name__ == '__main__':
    f = Feed('http://meat.net/test/feed', 'Test Feed', datetime.utcnow())
    f.add_author('David Terrell', 'http://meat.net/')
    f.close()
    print f.get()
